'use strict';

// constants 与 actions 在一起
import { ajax, nameSpace} from 'utils/index';

let ns = nameSpace('INDEX');
export const GET_TMC_DATA = ns('GET_TMC_DATA');
export const LOAD_ERR = ns('LOAD_ERR');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const LOAD_START = ns('LOAD_START');
export const GET_STATUS = ns('GET_STATUS');

export function closeDialog(tmcStatus) {
	return {type:CLOSE_DIALOG,tmcStatus};
}
export function getStatus(params) {  
	return (dispatch) => {
		ajax({
			  api: 'getStatus',
			  method: 'GET',
			  data:{sid:params.sid}
			},(json) => {
				dispatch({
					type: GET_STATUS,
					tmcStatus:json.status
				});
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}
}
export function permitTopics(topics) {
  return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START,
      data: topics
    });
	if(window.workbench){
		//千牛环境执行创建折扣,含有workbench参数为千牛环境
		  window.QN.top.invoke( {
				cmd : 'taobao.tmc.user.permit',
				param : {topics},
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'permitTopics',
			  method: 'GET',
			  data:topics
			},(json) => {
				dispatch({
					type: CLOSE_DIALOG,
					tmcStatus:1
				});
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}
export function cancleTmcUser(nick) {
  return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START
    });
	if(window.workbench){
		//千牛环境执行创建折扣,含有workbench参数为千牛环境
		  window.QN.top.invoke( {
				cmd : 'taobao.tmc.user.cancel',
				param : {nick},
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'cancelTmcUser',
			  method: 'GET',
			  data:{nick}
			},(json) => {
				dispatch({
					type: CLOSE_DIALOG,
					tmcStatus:0
				});
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}
export function getTmcState(params) {
  return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行创建折扣,含有workbench参数为千牛环境
		  window.QN.top.invoke( {
				cmd : 'taobao.tmc.user.get',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'getTmcState',
			  method: 'GET',
			  data:params
			},(json) => {
				if(json.tmc_user && json.tmc_user.topics){
					if(typeof(json.tmc_user.topics.string)=="string"){
						json.tmc_user.topics.string = [json.tmc_user.topics.string];
					}
				}
				const selectedTopics = (json.tmc_user && json.tmc_user.topics)?json.tmc_user.topics:[];
				dispatch({
					type: GET_TMC_DATA,
					data:json.tmc_user,
					topics:json.topics,
					selectedTopics
				});
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}


